package teacher;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.IOException;
import java.util.*;

//1.输出每部电影的总评分并查询总评分前3名(输出格式：电影编号  总评分)
public class score {
    public static class scoreMapper extends Mapper<LongWritable, Text, IntWritable, IntWritable> {
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            String[] split = line.split("::");
            int move = Integer.parseInt(split[1]);
            int score = Integer.parseInt(split[2]);
            context.write(new IntWritable(move), new IntWritable(score));
        }
    }

    public static class scoreReducer extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable> {
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();

        @Override
        protected void reduce(IntWritable key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            int sum = 0;
            for (IntWritable value : values) {
                sum += value.get();
            }
            map.put(key.get(), sum);
        }

        @Override
        protected void cleanup(Context context) throws IOException, InterruptedException {
            Set<Map.Entry<Integer, Integer>> set = map.entrySet();
            ArrayList<Map.Entry<Integer, Integer>> list = new ArrayList<Map.Entry<Integer, Integer>>(set);
            Collections.sort(list, new Comparator<Map.Entry<Integer, Integer>>() {
                public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {
                    return o2.getValue() - o1.getValue();
                }
            });
            for (int i = 0; i < Math.min(list.size(), 3); i++) {
                context.write(new IntWritable(list.get(i).getKey()), new IntWritable(list.get(i).getValue()));
            }
        }
    }

    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        Configuration config = new Configuration();
        Job job = Job.getInstance(config);
        job.setJarByClass(score.class);
        job.setMapperClass(scoreMapper.class);
        job.setReducerClass(scoreReducer.class);
        job.setMapOutputKeyClass(IntWritable.class);
        job.setMapOutputValueClass(IntWritable.class);
        job.setOutputKeyClass(IntWritable.class);
        job.setOutputValueClass(IntWritable.class);
        FileSystem fs = FileSystem.get(config);
        Path inputpath = new Path(Config.RATE_PATH);
        Path outputpath = new Path(Config.OUT_PATH[1] );
        if (fs.exists(outputpath)) {
            fs.delete(outputpath, true);
        }
        FileInputFormat.setInputPaths(job, inputpath);
        FileOutputFormat.setOutputPath(job, outputpath);
        boolean isdone = job.waitForCompletion(true);
        System.out.println(isdone ? "执行成功" : "执行失败");
    }

}
